<template>
    <div class="sifter-box flex-row-center" v-if="isEdit==0">
        <el-form :inline="true" :model="sifterForm" class="demo-form-inline">
            <el-form-item label="筛选：">
                <el-input v-model="sifterForm.key" placeholder="输入资讯关键字" @input="proxy.$utils.debounce(updalist,sifterForm)"></el-input>
            </el-form-item>
            <el-form-item>
                <el-date-picker v-model="sifterForm.time" type="daterange" value-format="YYYY-MM-DD" range-separator="至" start-placeholder="开始日期" end-placeholder="结束日期" align="right" size="small" @change="datePickerChange"></el-date-picker>
            </el-form-item>
        </el-form>
        <div>
            <el-button type="primary" plain :loading="false" @click.prevent="editData(null,1)">添加</el-button>
        </div>
    </div>

    <div class="listDiv" v-if="isEdit==0">
        <el-table :data="list" stripe style="width: 100%">
            <el-table-column prop="title" label="标题">
                <template #default="props">
                    <span class="base-color pointer" @click="editData(props.row,3)">{{props.row.title||''}}</span>
                </template>
            </el-table-column>
            <el-table-column prop="typename" label="类别" width="100"> </el-table-column>
            <el-table-column prop="intime" label="发布时间" width="180"> </el-table-column>
            <el-table-column prop="verify_state" label="审核状态" width="100">
                <template #default="props">
                    <span>{{props.row.verify_state=='0'?'待审核':(props.row.verify_state=='1'?'已通过':'未通过')}}</span>
                </template>
            </el-table-column>
            <el-table-column prop="" label="操作" width="100">
                <template #default="props">
                    <span v-if="props.row.isme!=0" class="pointer mr10" @click="editData(props.row,2)"><el-icon color="#409EFC"><edit/></el-icon></span>
                    <span v-if="props.row.isme!=0" class="pointer" @click="delData(props.row)"><el-icon color="#F56C6C"><delete /></el-icon></span>
                </template>
            </el-table-column>
        </el-table>
        <div class="pd20 w100 flex-row-center ju-flex-end">
            <el-pagination
                v-model:currentPage="sifterForm.currentpage"
                v-model:page-size="sifterForm.pagesize"
                :page-sizes="[10, 20, 50, 100]" :total="total"
                @size-change="()=>{updalist()}"
                @current-change="()=>{updalist()}"
                layout="total, sizes, prev, pager, next, jumper"  background/>
        </div>
    </div>

    <div class="edit-box" v-if="isEdit!=0">
        <div class="title-box flex-row-center">
            <span class="pointer return flex-row-center mr10" @click="re_turn"><el-icon><back /></el-icon></span>
            <span>{{isEdit==1?'添加':(isEdit==2?'编辑':'查看')}}企业资讯</span>
        </div>
        <div class="form-box" v-if="isEdit==1||isEdit==2">
            <el-form ref="ruleFormRef" :inline="true" :model="form" label-width="120px" class="demo-ruleForm">
                <el-form-item class="w96" :rules="[ { required: true, message: '标题不能为空'} ]" label="标题" prop="title">
                    <el-input autocomplete="off" clearable v-model="form.title"></el-input>
                </el-form-item>
                <br/>
                <el-form-item :rules="[ { required: true, message: '请选择类别'} ]" label="类别" prop="type">
                    <el-select v-model="form.type" placeholder="请选择" clearable>
                        <el-option v-for="item in types" :key="item.id" :label="item.name" :value="item.id">
                        </el-option>
                    </el-select>
                </el-form-item>
                <el-form-item :rules="[ { required: true, message: '发布时间不能为空'} ]" label="发布时间" prop="day">
                    <el-date-picker v-model="form.day" type="date" value-format="YYYY-MM-DD" placeholder="选择发布时间"></el-date-picker>
                </el-form-item>
                <br/>
                <el-form-item :rules="[ { required: true, message: '内容不能为空'} ]" label="内容" prop="msg">
                    <Editer @update:value="(e)=>{form.msg = e}" :value="form.msg"/>
                </el-form-item>
                <el-form-item label=" " label-width="120px">
                    <el-button type="primary" class="btn" :loading="false" @click.prevent="submit" native-type="submit">提交</el-button>
                </el-form-item>
            </el-form>
        </div>
        <div v-if="isEdit==3">
            <div class="flex-column overflow-y pd20">
                <div class="large-size weight flex-center mb20">{{form.title}}</div>
                <div class="flex-row-center mini-gray-size mb5">
                    <span class="mr20">类别：{{form.typename||''}}</span>
                    <span>发布时间：{{form.day||''}}</span>
                </div>
                <div class="line mb20"></div>
                <div v-html="form.msg"></div>
            </div>
        </div>
    </div>

</template>

<script setup>
    import { reactive,ref,getCurrentInstance,toRefs,toRaw } from 'vue'
    import { Back,Edit,Delete } from '@element-plus/icons-vue'
    import Editer from '../../components/editer.vue'

    let { proxy } = getCurrentInstance()
    let obj = reactive({
        isEdit: 0,
        total: 0,
        list:[],
        sifterForm: {
            key: '',
            time: '',
            starttime: '',
            endtime: '',
            currentpage: 1,
            pagesize: 10
        },
        form: {
            "id":"",
            "title":"",
            "type":"",
            "day":"",
            "msg":""
        },
        ruleFormRef: null,
        types: []
    });

    // 获取资讯类别
    proxy.$utils.getNewsType(proxy).then(res=>{
        types.value = res
    })

    const updalist = (form)=>{
        let param = {
            ...obj.sifterForm,
            ...form
        }
        proxy.$ajax({url: 'news/list',data: param,bfLoading: true}).then(d => {
            console.log(d);
            if(d.success){
                total.value = d.msg.count||0
                list.value = d.msg.list;
            }else{}
        }).catch(e => {
            console.log(e)
            if(e == "登录已过期，请重新登录"){
                proxy.routerPushMixin('/')
            }
        })
    }
    updalist();

    const datePickerChange = (e)=>{
        if(e&&e.length==2){
            sifterForm.value.starttime = e[0]
            sifterForm.value.endtime = e[1]
            updalist(obj.sifterForm)
        }
    }

    const delData = (item)=>{
            proxy.$ajax({url: 'news/delete',data:{id:item.id},bfLoading: true}).then(d => {
                console.log(d);
                if(d.success){
                    proxy.elMessageMixin('删除成功！', 'success')
                    updalist()
                }else{proxy.elMessageMixin(date.err, 'error')}
            }).catch(e => {
                if(e == "登录已过期，请重新登录"){
                    proxy.routerPushMixin('/')
                }
            })
    }

    const re_turn = ()=>{
        isEdit.value = 0;
        form.value = {
            "id":"",
            "title":"",
            "type":"",
            "day":"",
            "msg":""
        }
    }

    const editData = (item,isEdit_)=>{
        if(item){
            proxy.$ajax({url: 'news/get',data:{id:item.id},bfLoading: true}).then(d => {
                // console.log(d);
                if(d.success){
                    form.value = {
                        ...d.msg,
                        id: item.id,
                        typename: item.typename
                    }
                    isEdit.value = isEdit_;
                }else{}
            }).catch(e => {
                console.log(e)
                if(e == "登录已过期，请重新登录"){
                    proxy.routerPushMixin('/')
                }
            })
        }else{
            isEdit.value = isEdit_
        }
    }

    const submit = ()=>{
      if (ruleFormRef) {
        ruleFormRef.value.validate((valid) => {
          if (valid) {
                console.log(form.value);
                let param = {
                    ...form.value,
                    id:form.value.id||0
                }
                // return
                proxy.$ajax({url: 'news/design',data:param,bfLoading: true}).then(d => {
                    console.log(d);
                    if(d.success){
                        form.value = {
                            "id":"",
                            "title":"",
                            "type":"",
                            "day":"",
                            "msg":""
                        }
                        proxy.elMessageMixin('编辑成功！', 'success')
                        isEdit.value = 0
                        updalist()
                    }else{proxy.elMessageMixin(date.err, 'error')}
                }).catch(e => {
                    console.log(e)
                    if(e == "登录已过期，请重新登录"){
                        proxy.routerPushMixin('/')
                    }
                })
          } else {
            return false;
          }
        });
      }
    }
    
    let { list, sifterForm, form, isEdit, ruleFormRef, types, total } = toRefs(obj)
</script>

<style lang="scss" scoped>
</style>